
*************************************************************************
** 							GENDER AND WEALTH						   **
**					Doron Shiffer-Sebba and Julia Behrman 			   **
**						Last updated: 10/19/2020, DSS				   **
*************************************************************************


***************************************************************************
*** SETUP *****************************************************************
***************************************************************************	 

version 14.2
clear
capture log close
set more off

global path    "/localpath"   /* Adjust to user project folder path */

log using ""$path/output/3", replace

***************************************************************************
*** CONTENTS **************************************************************
***************************************************************************

/*

- Merge main data with genderized data
	- Import genderized data
	- Prepare genderized data for merge
	- Import main data
	- Prepare main data for merge
	- Merge main data with genderized data
- Merge with ACS data
	- Prepare variables
	- Create indicator for observations not having ACS values
- Reshape data to household level
- Construct homeownership type variables
	- Create dummy variables for ownership types
	- Create categorical variable for ownership types
	- Create alternative ownership type variable
	- Create additional ownership type indicators for tables
- Construct dummy variable for confidence in gender prediction
	- Construct 91% Confidence Variable
	- Construct 95% Confidence Variable
	- Construct 98% Confidence Variable
- Merge data with inflation index
- Clean variables
	- Label cities
	- Harmonize and rename variables
	- Create inflation adjusted price variable
		- Change to 2018 dollars
	- Create homestead indicator
- Clean dataset
- Define sample
- Save

*/

////////////////////////////////////////
* Merge main data with Genderized Data
////////////////////////////////////////

import delimited using "$path/data/genderized", clear			// Prepare gender data for merge
tostring year_min year_max, generate(strmin strmax)				// Prepare gender data for merge
gen uniqueid = name + strmin + strmax
drop strmin strmax
save "$path/data/genderized", replace							// Export gender data

import delimited using "$path/data/clean1", clear				// Import main data
tostring minbirth maxbirth, generate(strmin strmax)				// Prepare main data for merge
gen uniqueid = first + strmin + strmax
merge m:1 uniqueid using "$path/data/genderized", nogen			// Merge with gender predictions
drop uniqueid v1 name agerange year_min year_max strmin strmax	// Clean unnecessary variables


////////////////////////////////////////////////////////////////////////
* Merge Tax Assessor Data with ACS 2013-2017 Estimates using Zipcodes
////////////////////////////////////////////////////////////////////////

/* 	Merge and define the following ACS variables:				//  Prepare Variables
	- permarriedhh = % Married in Zipcode
	- percenthisp = % Hispanic in Zipcode
	- perblack = % Black in Zipcode
	- persomecollege = % Attended College in Zipcode
	- perover100k = % Earning over $100k in Zipcode
*/
g hasnoacs = 1 if [insert variable] == .						// Create indicator for observations not having ACS values


/////////////////////////////////////
* 	Reshape Data to household Level
/////////////////////////////////////

reshape wide fullname firstname percentown pid proportion_male proportion_female ///
	gender, i(houseid) j(person)

	
//////////////////////////////////////////
* 	Construct Homeownership Type Variable
//////////////////////////////////////////
	
egen numowners = rownonmiss(proportion_male*)				  // Create household gender ownership dummy variable
g malesolo = 1 if numowners == 1 & gender1 == "male"
g femalesolo = 1 if numowners == 1 & gender1 == "female"
g mixed = 1 if numowners == 2 & (gender1 == "male" & gender2 == "female" ///
	| gender1 == "female" & gender2 == "male")

g ownertype = 1 if malesolo == 1							 // Create equivalent categorical variable
replace ownertype = 2 if femalesolo == 1
replace ownertype = 3 if mixed == 1
replace ownertype = 4 if ownertype == .
label define ownlab 1 "Male" 2 "Female" 3 "Mixed" 4 "Other", replace
label values ownertype ownlab 

g hasmale = 1 if ownertype == 1								// Create alternative ownership type variable
g hasfemale = 1 if ownertype == 2
forvalues i = 1/4	{
	replace hasmale = 1 if gender`i' == "male"
	replace hasfemale = 1 if gender`i' == "female"
	}
replace hasmale = 0 if hasmale == . 
replace hasfemale = 0 if hasfemale == . 

label var hasfemale "female owner"
label define hasfemlab 0 "no female" 1 "female ", replace
label values hasfemale hasfemlab

g malesolomixed = 1 if malesolo == 1						// Create additional ownership type indicators for tables
replace malesolomixed = 0 if mixed == 1
label define malesolomixed 0 "Pair" 1 "Male Solo"
label values malesolomixed malesolomixed
g femalesolomixed = 1 if femalesolo == 1
replace femalesolomixed = 0 if mixed == 1
label define femalesolomixed 0 "Pair" 1 "Female Solo"
label values femalesolomixed femalesolomixed
g otherfemale = 1 if ownertype == 4
replace otherfemale = 0 if femalesolo == 1
label define otherfemale 0 "Female Solo" 1 "Other"
label values otherfemale otherfemale
g malefemale = 1 if malesolo == 1
replace malefemale = 0 if femalesolo == 1
label define malefemale 0 "Female Solo" 1 "Male Solo"
label values malefemale malefemale
g mixedother = 1 if mixed == 1
replace mixedother = 0 if ownertype == 4
label define mixedother 0 "Other" 1 "Pair"
label values mixedother mixedother


///////////////////////////////////////////////////////////////////
* 	Construct Dummy Variable for Confidence in Gender Prediction
///////////////////////////////////////////////////////////////////

g hasless91 = 0											// Generate dummy for 91% Confidence
forvalues i=1/4 {
	replace hasless91 = 1 if fullname`i' != "" & ///
	(((proportion_male`i' < 0.91 & proportion_male`i' > 0.09) &  ///
	(proportion_female`i' < 0.91 & proportion_female`i' > 0.09)) | ///
	(proportion_male`i' == . & proportion_female`i' == .)) 
	}
g hasless95 = 0											// Generate dummy for 95% Confidence
forvalues i=1/4 {
	replace hasless95 = 1 if fullname`i' != "" & ///
	(((proportion_male`i' < 0.95 & proportion_male`i' > 0.05) &  ///
	(proportion_female`i' < 0.95 & proportion_female`i' > 0.05)) | ///
	(proportion_male`i' == . & proportion_female`i' == .)) 
	}
g hasless98 = 0											// Generate dummy for 98% Confidence
forvalues i=1/4 {
	replace hasless98 = 1 if fullname`i' != "" & ///
	(((proportion_male`i' < 0.98 & proportion_male`i' > 0.02) &  ///
	(proportion_female`i' < 0.98 & proportion_female`i' > 0.02)) | ///
	(proportion_male`i' == . & proportion_female`i' == .)) 
	}

//////////////////////////////////////
* 	Merge Data with Inflation Index
//////////////////////////////////////

merge m:1 year using "$path/data/inflation_index", nogen keep(match master) // Merge with 1968 dollar index


//////////////////////
* 	Clean Variables
//////////////////////

// Cities
encode city, gen (city3)								// Attach labels to homeownership type variable
ta city3, gen (c)
	rename c1 Detroit
	rename c2 Philadelphia
label define cities 1 "Detroit" 2 "Philadelphia"
label val city3 cities

// Property size
g prop_size = sqft								
replace prop_size = total_area if sqft == .

// Sale Year
rename year sale_year

// Current Property Value 
replace currentpropertyvalue = imp_value if currentpropertyvalue == . & imp_value != .

// Purchased Price
gen purchased_inf = purchasedprice/inflation       	    // Create inflation adjusted variable
replace purchasedprice = purchased_inf*7.27 	    	// Change to 2018 dollars

// Label Variables
label var prop_size "Property Size"
label var sale_year "Sale Year"
label var currentpropertyvalue "Current Value"
label var purchasedprice "Purchase Price"

// Homestead Exemption
g hmstd = 1 if homesteadexemption > 0 & homesteadexemption != .			// Create homestead indicator

/////////////////////////
* 	Clean Dataset
/////////////////////////

keep hmstd purchasedprice currentpropertyvalue sale_year prop_size city3 ///
	 hasless91 hasless95 hasless98 malesolomixed femalesolomixed otherfemale ///
	 malefemale mixedother hasfemale hasmale ownertype mixed femalesolo ///
	 malesolo houseid permarriedhh percenthisp perblack persomecollege ///
	 perover100k 

/////////////////////////
* 	Define Main Sample
/////////////////////////

g sample = 1 if hasnoacs != 1 & hasless91 == 0

///////////////////
* 	Save Dataset
///////////////////

save "$path/data/final", replace


clear
capture log close
exit



